package com.neusoft.elm.service.impl;

import java.util.ArrayList;
import java.util.List;
import com.neusoft.elm.dao.UserDao;
import com.neusoft.elm.dao.impl.UserDaoImpl;
import com.neusoft.elm.po.User;
import com.neusoft.elm.service.UserService;
import com.neusoft.elm.util.DBUtil;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;

public class UserServiceImpl implements UserService {
	@Override
	public User getUserByIdByPass(String userId, String password) {
		User user = null;
		UserDao dao = new UserDaoImpl();
		try {
			DBUtil.getConnection();
			user = dao.getUserByIdByPass(userId, password);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close();
		}
		return user;
	}

	@Override
	public int getUserById(String userId) {
		int result = 0;
		UserDao dao = new UserDaoImpl();
		try {
			DBUtil.getConnection();
			result = dao.getUserById(userId);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close();
		}
		return result;
	}

	@Override
	public int saveUser(User user) {
		int result = 0;
		UserDao dao = new UserDaoImpl();
		try {
			DBUtil.getConnection();
			result = dao.saveUser(user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBUtil.close();
		}
		return result;
	}
	@Override
    public int updateUserPassword(String userId, String Oldpassword, String Newpassword) {
        int result = 0;
        UserDao dao = new UserDaoImpl();
        try {
            DBUtil.getConnection();
            // 首先验证旧密码是否正确
            User user = dao.getUserByIdByPass(userId,Oldpassword);
            if (user != null && user.getPassword().equals(Oldpassword)) {
                // 更新密码
                result = dao.updateUserPassword(userId, Newpassword);
            } else {
                // 旧密码不正确
                result = -1; // 可以根据实际情况定义错误码
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = -2; // 可以根据实际情况定义错误码
        } finally {
            DBUtil.close();
        }
        return result;
    }
	 @Override
	 public boolean saveUserAvatarFromInputStream(String userId, InputStream inputStream) {
	        if (inputStream == null) {
	            return false;
	        }
	        try {
	            // 定义头像文件的保存路径
	            String uploadDir = "/path/to/avatars/"; // 确保这个目录存在且应用有写权限
	            File uploadDirFile = new File(uploadDir);
	            if (!uploadDirFile.exists()) {
	                uploadDirFile.mkdirs(); // 如果目录不存在，则创建目录
	            }
	            String fileName = userId + "_avatar.png"; // 假设头像文件为PNG格式
	            String filePath = uploadDir + fileName;
	            File file = new File(filePath);

	            // 将输入流写入文件
	            try (OutputStream outputStream = new FileOutputStream(file)) {
	                byte[] buffer = new byte[4096];
	                int bytesRead;
	                while ((bytesRead = inputStream.read(buffer)) != -1) {
	                    outputStream.write(buffer, 0, bytesRead);
	                }
	            }

	            // 更新数据库中的用户头像路径
	            UserDao userDao = new UserDaoImpl();
	            int result = userDao.updateUserAvatar(userId, filePath);
	            return result > 0;
	        } catch (Exception e) {
	            e.printStackTrace();
	            return false;
	        }
	    }
}
